package com.app.brilliant.util;

import com.aliyuncs.utils.IOUtils;

import java.io.FileInputStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class StringUtils {
    private static int jinWeiSum = 10;
    public static String getNextVersion(String gameVersion) {
       String versions[] = gameVersion.split("\\.");
       List<Integer> strings = Arrays.asList(versions).stream().map(x->Integer.parseInt(x)).collect(Collectors.toList());
       Collections.reverse(strings);
       jinWei(strings,0,false);
       Collections.reverse(strings);
       return strings.stream().map(String::valueOf).collect(Collectors.joining("."));
    }

    public static boolean isEmpty(String str){
        return str == null||"".equals(str.trim());
    }
    private static void jinWei(List<Integer> list,int currentIndex,boolean jinwei){
        if(currentIndex == list.size()){
            return;
        }
        if(currentIndex == 0||jinwei){
            int value = list.get(currentIndex)+1;
            list.set(currentIndex,value);
        }
        int value = list.get(currentIndex);
        jinwei = value == jinWeiSum;
        if(jinwei){
            list.set(currentIndex,0);
        }
        StringUtils.jinWei(list,++currentIndex,jinwei);
    }

    public static String getLastName(String path){
        return path.substring(path.lastIndexOf("/")+1);
    }

    public static String getBundleVersion(String zipPath)  {
        ZipInputStream zipInputStream = null;
        String bundleVersion = null;
        try {
             zipInputStream = new ZipInputStream(new FileInputStream(zipPath));
             ZipEntry zipEntry = null;
             while((zipEntry = zipInputStream.getNextEntry())!=null){
                if(!zipEntry.isDirectory()){
                    String name = zipEntry.getName();
                    String strRegex = ".*config\\.([0-9a-z]{5})\\.json";
                    if(name.matches(strRegex)){
                        Pattern pattern = Pattern.compile(strRegex);
                        Matcher matcher = pattern.matcher(name);
                        if(matcher.find()){
                            bundleVersion = matcher.group(1);
                            break;
                        }
                    }
                }
             }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            IOUtils.closeQuietly(zipInputStream);
        }
        return bundleVersion;
    }

    public static String getRandomPwd(){
        Random rd = new Random();
        String n = "";
        int getNum;
        int getNum1;
        do {
            getNum = Math.abs(rd.nextInt()) % 10 + 48;// 产生数字0-9的随机数
            getNum1 = Math.abs(rd.nextInt())%26 + 97;//产生字母a-z的随机数
            char num1 = (char) getNum;
            char num2 = (char) getNum1;
            String dn = Character.toString(num1);
            String dn1 = Character.toString(num2);
            if(Math.random()>0.5){
                n += dn;
            }else{
                n += dn1;
            }
        } while (n.length() < 8 );
        return n;
    }

    public static Map<String,String> getMapFromParams(Map<String,String[]> params){
        Map<String,String> map = new HashMap<>();
        if(params != null){
            params.forEach((key,value)->{
                if(value != null ){
                    StringBuilder stringBuilder = new StringBuilder();
                    for(int i = 0;i<value.length;i++){
                        stringBuilder.append(value[i]+",");
                    }
                    int len = stringBuilder.toString().length();
                    String des = len == 0?"":stringBuilder.toString().substring(0,len-1);
                    if(!StringUtils.isNull(des)){
                        map.put(key,des);
                    }
                }
            });
        }
        return map;
    }

    public static boolean isNull(String str){
        return str == null||"".equals(str.trim());
    }
}
